package top.superflow.flow.runtime.mapper

import top.superflow.exception.Exceptable
import top.superflow.flow.runtime.Mappable
import top.superflow.flow.runtime.flow.{FlowRequest, FlowResponse}
import top.superflow.flow.runtime.inf.IOSchema
import top.superflow.flow.runtime.process.ProcessRequest


trait RuntimeMapper extends Mappable[FlowRequest, FlowResponse, ProcessRequest] with Exceptable{


  private var currentInputSchema : IOSchema = _

  protected def getCurrentInputSchema : IOSchema =  {
    if(this.currentInputSchema == null){
      throwRequestUnsupportedException("Please ensure the method init has been invocated.")
    }
    this.currentInputSchema
  }


  def init(currentProcessInputSchema : IOSchema): Unit ={
    if(currentProcessInputSchema == null){
      throwRequestUnsupportedException("The current process input schema can't be null!")
    }

    this.currentInputSchema = currentProcessInputSchema
  }

  final def map(flowRequest: FlowRequest, flowResponse: FlowResponse): ProcessRequest = {
    if(flowRequest == null){
      throwRequestUnsupportedException("The flow request can't be null!")
    }

    if(flowResponse == null){
      throwRequestUnsupportedException("The flow response can't be null!")
    }

    val processRequest = generatedProcessRequest(flowRequest, flowResponse)


    processRequest
  }

  protected def generatedProcessRequest(flowRequest: FlowRequest, flowResponse: FlowResponse): ProcessRequest


}